package com.to8to.scene.datatransfer;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryOperators;
import com.to8to.commons.mongo.MongoClientBase;
import com.to8to.commons.utils.Progress;
import com.to8to.commons.utils.ThriftUtil;
import com.to8to.scene.data.DataFixIface;
import com.to8to.scene.server.Commons;
import com.to8to.scene.server.Tables;
import com.to8to.thrift.diary.module.TDiary;

/**
 * FixDiaryTags.java
 * 作 者:alpha.ren
 * 版 本:v2.0
 * 日期:2015年1月9日
 * 描 述:修正日记数据中碎碎念问题
 */
public class FixDiaryTags implements DataFixIface
{
    public static Logger logger = LoggerFactory.getLogger(FixDiaryTags.class);

    static Connection conn;

    public void fix(Connection mysql, MongoClientBase mongo) throws SQLException
    {
        DBObject query = new BasicDBObject();

        //1.碎碎念
        query.put(TDiary._Fields.TAG_ID.getFieldName(), new BasicDBObject(QueryOperators.IN, Arrays.asList(new Integer[] { 501, 502, 503, 504, 505, 506, 507, 508 })));

        DBCursor diary_cur = Commons.mongo.cursor(Tables.Diary, query, null, new BasicDBObject(TDiary._Fields.CTIME.getFieldName(), -1), 0, 0);

        int i = 0;
        logger.info("{} origin diaries' tag need tobe updated for 碎碎念!", diary_cur.count());
        Progress progress = new Progress(diary_cur.count());
        while (diary_cur.hasNext())
        {
            DBObject diary_db = diary_cur.next();
            //            logger.info("diary:{}", diary_db);
            if (null == diary_db)
                continue;
            TDiary diary = ThriftUtil.dbObject2ThriftObject(diary_db, TDiary.class);
            progress.increase(logger);

            mongo.updateField(Tables.Diary, diary.get_id(), TDiary._Fields.TAG_ID.getFieldName(), 51);
            logger.info("update diary {} on id:{}", 51, diary.get_id());
        }

        //2.产品选购
        query.put(TDiary._Fields.TAG_ID.getFieldName(), new BasicDBObject(QueryOperators.IN, Arrays.asList(new Integer[] { 4, 8, 11, 16, 20, 32, 38 })));

        diary_cur = Commons.mongo.cursor(Tables.Diary, query, null, new BasicDBObject(TDiary._Fields.CTIME.getFieldName(), -1), 0, 0);

        logger.info("{} origin diaries' tag need tobe updated for 产品选购!", diary_cur.count());
        progress = new Progress(diary_cur.count());
        while (diary_cur.hasNext())
        {
            DBObject diary_db = diary_cur.next();
            //            logger.info("diary:{}", diary_db);
            if (null == diary_db)
                continue;
            TDiary diary = ThriftUtil.dbObject2ThriftObject(diary_db, TDiary.class);
            progress.increase(logger);

            mongo.updateField(Tables.Diary, diary.get_id(), TDiary._Fields.TAG_ID.getFieldName(), 50);
            logger.info("update diary {} on id:{}", 50, diary.get_id());
        }
    }

    public static void main(String[] args) throws SQLException, ConfigurationException
    {
//        PropertiesConfiguration pc = new PropertiesConfiguration("common.properties");

        Commons.init(null);

        (new FixDiaryTags()).fix(conn, Commons.mongo);

        conn.close();
    }
}
